package com.wpw.java7demo.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class FilesTest {
	
	Path path = Paths.get("test.txt").toAbsolutePath();

	@Test
	public void test1() {
		try {
			byte[] bytes = Files.readAllBytes(path);
			System.out.println("长度" + bytes.length);
			System.out.println(new String(bytes, StandardCharsets.UTF_8));
			System.out.println("==========");
			List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
			for (String line : allLines) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test2() {
		try {
			Files.write(path, "第四行\n".getBytes());

			List<String> lines = new ArrayList<>();
			lines.add("第一行");
			lines.add("第二行");
			lines.add("第三行");
			Files.write(path, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);

			//Files.delete(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void Test3() {
		try {
			InputStream newInputStream = Files.newInputStream(path);
			OutputStream newOutputStream = Files.newOutputStream(path);
			
			BufferedReader newBufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
			BufferedWriter newBufferedWriter = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void test4() throws IOException {
		Path path2 = Paths.get("test2.txt");
		if (path2.toFile().exists()) Files.delete(path2);
		path2 = Files.createFile(path2);
		
		OutputStream outputStream = Files.newOutputStream(path2, StandardOpenOption.APPEND);
		Files.copy(path, outputStream);
		
		InputStream inputStream = Files.newInputStream(path2);
		Path path3 = Paths.get("test3.txt");
		if (path3.toFile().exists()) Files.delete(path3);
		
		Files.copy(inputStream, path3);
	}

	@Test
	public void test5() throws IOException {
		Path p = Paths.get("newPath");
		Files.deleteIfExists(p);
		//路径中除了最后一部分目录，其他部分必须全部存在
		Files.createDirectory(p);
		
	}
	
	@Test
	public void test6() throws IOException {
		Path pp = Paths.get("newPath2\\next\\next2");
		if (Files.exists(pp)) Files.delete(pp);
		Files.createDirectories(pp);
		//不能直接删除非空目录
//		Files.delete(pp.getParent().getParent());
	}
	
	@Test
	public void test7() throws IOException {
		Files.deleteIfExists(Paths.get("copy.txt"));
		Files.copy(path, Paths.get("copy.txt"));
		
		Files.deleteIfExists(Paths.get("move.txt"));
		Files.move(Paths.get("copy.txt"), Paths.get("move.txt"));
	}
	
	@Test
	public void test8() {
	}
	
}
